package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.QueryAchievementTeamListReq;
import com.sg.entity.OmsAchievementTeam;
import com.sg.service.base.app.MOmsAchievementTeamService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsAchievementTeamMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsAchievementTeamServiceImpl
        extends ServiceImpl<OmsAchievementTeamMapper, OmsAchievementTeam>
        implements MOmsAchievementTeamService {

    @Autowired
    OmsAchievementTeamMapper omsAchievementTeamMapper;

    /**
     * 3-10-06新增成果团队[9125]
     * gen by moon at 4/3/2024, 6:43:27 PM
     */
    @Trace(operationName = "3-10-06新增成果团队")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addAchievementTeam(OmsAchievementTeam omsAchievementTeam) {
        omsAchievementTeam.setAchievementTeamId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsAchievementTeam.setIsArchive("FALSE");
        omsAchievementTeam.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsAchievementTeam.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAchievementTeam != null && omsAchievementTeam.getSubjectLifeCycle() != null && omsAchievementTeam.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementTeam.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsAchievementTeam.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsAchievementTeam.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAchievementTeam != null && omsAchievementTeam.getSubjectLifeCycle() != null && omsAchievementTeam.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementTeam.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsAchievementTeam.setIsValid("FALSE");
        if (super.save(omsAchievementTeam)) {
            return omsAchievementTeam.getAchievementTeamId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-10-06修改成果团队[9126]
     * gen by moon at 4/3/2024, 6:43:27 PM
     */
    @Trace(operationName = "3-10-06修改成果团队")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAchievementTeam(OmsAchievementTeam omsAchievementTeam) {
        omsAchievementTeam.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAchievementTeam != null && omsAchievementTeam.getSubjectLifeCycle() != null && omsAchievementTeam.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementTeam.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsAchievementTeam != null && omsAchievementTeam.getSubjectLifeCycle() != null && omsAchievementTeam.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAchievementTeam.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsAchievementTeam.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAchievementTeam != null && omsAchievementTeam.getSubjectLifeCycle() != null && omsAchievementTeam.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementTeam.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsAchievementTeam != null && omsAchievementTeam.getSubjectLifeCycle() != null && omsAchievementTeam.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAchievementTeam.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsAchievementTeam);
    }

    /**
     * 3-10-06查询成果团队列表[9127]
     * gen by moon at 4/3/2024, 6:43:31 PM
     */
    @Trace(operationName = "3-10-06查询成果团队列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAchievementTeam> queryAchievementTeamList(QueryAchievementTeamListReq reqDto) {

        try {
            QueryWrapper<OmsAchievementTeam> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAchievementTeam.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAchievementTeam.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAchievementTeam.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("create_time");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsAchievementTeam>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-10-06删除成果团队[9128]
     * gen by moon at 4/3/2024, 6:43:32 PM
     */
    @Trace(operationName = "3-10-06删除成果团队")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteAchievementTeam(String achievementTeamId) {

        return super.removeById(achievementTeamId);
    }
}
